<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<button class="btn btn-info" id="btn-add">
	<span class="glyphicon glyphicon-plus">新增员工</span>
</button>
<button class="btn btn-danger" id="btn-del">
	<span class="glyphicon glyphicon-remove">删除</span>
</button>

<table class="table">
	<thead>
		<tr>
			<th><input type="checkbox" id="checkedAll" /></th>
			<th>编号</th>
			<th>工号</th>
			<th>姓名</th>
			<th>月薪</th>
			<th>时间</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody>
		<c:forEach items="${emps }" var="emp" varStatus="state">
			<tr>
				<td><input type="checkbox" name="empIds"
					value="${emp.emp_id }" /></td>
				<td>${state.count }</td>
				<td>${emp.emp_id }</td>
				<td>${emp.emp_name }</td>
				<td>${emp.emp_salary }</td>
				<td>${emp.emp_date }</td>
				<td>
					<button class="btn btn-info" onclick="updateEmp(${emp.emp_id});">
						<span class="glyphicon glyphicon-edit">修改</span>
					</button>
					<button class="btn btn-danger" onclick="deleteEmp(${emp.emp_id});"
						data-id="${emp.emp_id }">
						<span class="glyphicon glyphicon-remove">删除</span>
					</button>
				</td>
			</tr>
		</c:forEach>
	</tbody>
</table>
<script type="text/javascript">
	var layIdx;
	//添加
	$('#btn-add').click(function(){
		$.ajax({
			url:'addEmp.jsp',
			method:'get',
			cache:false,
			dataType:'html'
		}).done(function(html){
			layIdx = layer.open({
			  type: 1, 
			  content:html, //这里content是一个普通的String
			  area:['800px','540'],
			  title: ['新增员工信息', 'font-size:18px;color:coral;font-weight:bold;']
			});
		});
		return false;
	});
	//修改员工信息
	function updateEmp(id){
		alert(id);
		$.ajax({
			url:'empServlet?operate=updateEmpById',
			method:'post',
			data:{empId:id},
			dataType:'html'
		}).done(function(html){
			layIdx = layer.open({
				  type: 1, 
				  content:html, //这里content是一个普通的String
				  area:['800px','540'],
				  title: ['修改员工信息', 'font-size:18px;color:coral;font-weight:bold;']
				});
		});
	};
	
	//删除员工
	function deleteEmp(id){
		$.ajax({
				url:'empServlet?operate=deleteEmp',
				method:'post',
				data:{empId:id},
				dataType:'json'
			}).done(function(json){
				if(json.msg=="success"){
					loadEmpInfo();
					swal({
						  title: json.msg,
						  icon: "success",
						  button: "确定"
					});
				}
			});
	}
	
	//全选
	$("#checkedAll").click(function(){
		$(':checkbox[name=empIds]').prop("checked",this.checked);
	});
	
	//删除多个员工信息
	$("#btn-del").click(function(){
		//获取员工id
		var ids = [];//js数组
		$(':checkbox[name=empIds]:checked').each(function(){
			ids.push($(this).val());
		});
		swal({
			title: "您确实要删除该条数据吗？",
			text: "一旦删除，数据将不可恢复!",
			icon: "warning",
			dangerMode: true,
			buttons: [
				"取消",
				{
					text:'确认删除',
					closeModal: false
				}
			],
			
		})	
		.then(function(ok){
			if(ok){
				
				$.ajax({
					url:'empServlet?operate=deleteEmps',
					data:{empIds:ids},
					method:'post',
					dataType:'json'
				}).done(function(json){
					
					if(json.msg=="success"){
						loadEmpInfo();
						swal({
							  title: json.msg,
							  icon: "success",
							  button: "确定"
						});
					}else{
						swal({
							  title: json.msg,
							  icon: "error",
							  button: "确定"
						});
					}
					
					
				}).fail(function(xhr){
					swal({
						  title: xhr.status,
						  text:'请与系统管理员联系。',
						  icon: "error",
						  button: "确定"
					});
				});
				
			}
		});
	});
	
	
	
	
	
	//删除员工
	/* function deleteEmp1(id){
		swal(
				{
				  title: "您确实要删除该条数据吗？",
				  text: "一旦删除，数据将不可恢复!",
				  icon: "warning",
				  //buttons: true,
				  dangerMode: true,
				  buttons: ["取消", "确认删除"],
				}
			).then(function(ok){
				if(ok){
					$.ajax({
						url:'empServlet?operate=deleteEmp',
						method:'post',
						data:{empId:id},
						dataType:'json'
					}).done(function(json){
						if(json.success){
							loadEmpInfo();
							swal({
								  title: json.msg,
								  icon: "success",
								  button: "确定"
							});
						}else{
							swal({
								  title: json.msg,
								  icon: "error",
								  button: "确定"
							});
						}
					}).fail(function(xhr){
						swal({
							  title: xhr.status,
							  text:'请于管理员联系',
							  icon: "error",
							  button: "确定"
						});
					});
				}
			});
	}; */
	
</script>